import shutil

import pandas as pd
import os
import warnings


def func_rename_do(path_input):
    path_input_name = path_input + '\\detectImage'
    path_input_name = os.listdir(path_input_name)
    path_input_name = [int(x) for x in path_input_name if '-' not in x]
    path_input_name = sorted(path_input_name)
    # print(path_input_name)

    for root_dirs, sub_dirs, files in os.walk(path_input):
        # 仅读取时间戳  对时间戳进行正序排序
        files = [str(x).split('.')[0] for x in files if x.endswith('dat')]
        files = list(map(int, files))
        files = sorted(files)
        if len(files) != len(path_input_name) and len(files) != 0:
            print('数据文件数量不对应：', path_input, len(files), len(path_input_name))
        elif len(files) == len(path_input_name) and len(files) != 0:
            for i in range(len(files)):
                # 进行条件筛选   以非~$'开头(解决读取文档已打开问题)；选择以file_name_0结尾的文档
                # if (file.startswith('~$') is False) and (file.lower().endswith('data.xlsx') is False):
                file = os.path.join(root_dirs, str(files[i])) + '.dat'
                # new_file = root_dirs + '\\' + file.split('_')[-1]  # 转化为时间戳 配合sub_dir 进行使用
                # new_file = root_dirs + '\\' + str(i + 1) + '_' +str(i + 1) + '_'+ k + '_' + str(file_l).split('_')[-1]+'.dat'
                new_file = root_dirs + '\\' + str(path_input_name[i]) + '.dat'
                print(file)
                print(new_file)
                # try:
                #      if os.path.isdir(file):
                #          os.rename(file, new_file)
                #          print('1', file)
                #      else:
                #          os.rename(file, new_file)
                #          print('2', file, new_file)
                # except Exception as e:
                #      print(e)
                #      pass
                #

def func_rename(path_inputs):
    for root_ds, sub_ds, files in os.walk(path_inputs):
        for file in sub_ds:
            if file.endswith('Data') and ('Kongsao' not in root_ds) and ('kongsao' not in root_ds):
                file_path = os.path.join(root_ds, file)
                print('目标文件夹', file_path)
                func_rename_do(file_path)


if __name__ == "__main__":
    path_inputs = r"V:\yebaoyi\TAI-40-II\20210809RadarData\1#\test2"
    func_rename(path_inputs)  # 对bmp文件夹进行重命名，仅读取时间戳进行排序，可能找不到文件夹（原文件夹需为时间戳）
    print('-----------进程已结束-----------')
